﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace WebSpiderLib
{
    public class WebRegEx
    {
        // Methods
        private WebRegEx()
        {
        }

        public static ArrayList GetRepeatingValuesByPattern(string input, string pattern, string groupName)
        {
            ArrayList list = new ArrayList();
            for (Match match = Regex.Match(input, pattern, RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase); match.Success; match = match.NextMatch())
            {
                if (match.Groups[groupName] != null)
                {
                    list.Add(match.Groups[groupName].Value);
                }
            }
            return list;
        }

        public static string GetValueByPattern(string input, string pattern, string groupName)
        {
            try
            {
                Match match = Regex.Match(input, pattern, RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
                if (!(match.Success && (match.Groups[groupName] != null)))
                {
                    return null;
                }
                return match.Groups[groupName].Value;
            }
            catch
            {
                return null;
            }
        }

        public static NameValueCollection GetValuesByPattern(string input, string pattern, string[] groupNames)
        {
            Match match = Regex.Match(input, pattern, RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                return null;
            }
            NameValueCollection values = new NameValueCollection(groupNames.Length);
            foreach (string str in groupNames)
            {
                values.Add(str, match.Groups[str].Value);
            }
            return values;
        }

        private static string Htmlcode_MatchEvaluator(Match match)
        {
            Group group = match.Groups["value"];
            if (group == null)
            {
                return "";
            }
            switch (group.Value)
            {
                case "amp":
                    return "&";

                case "nbsp":
                    return " ";
            }
            return new string(Convert.ToChar(Convert.ToUInt16(group.Value)), 1);
        }

        public static string HtmlcodeToUnicode(string htmlContent)
        {
            return Regex.Replace(htmlContent, @"&#(?<value>(\d*|amp|nbsp));", new MatchEvaluator(WebRegEx.Htmlcode_MatchEvaluator), RegexOptions.IgnoreCase);
        }

        public static string RemoveDocTag(string input)
        {
            string strInputHtml = "";
            try
            {
                strInputHtml = RemoveTag(input);
                strInputHtml = RemoveHTML(strInputHtml);
            }
            catch
            {
            }
            return strInputHtml;
        }

        public static string RemoveDocTag(string input, string Exception)
        {
            string strInputHtml = "";
            try
            {
                strInputHtml = RemoveTag(input, Exception);
                strInputHtml = RemoveHTML(strInputHtml);
            }
            catch
            {
            }
            return strInputHtml;
        }

        private static string RemoveHTML(string strInputHtml)
        {
            //StringReader reader = new StringReader(strInputHtml);
            //HtmlReader reader2 = new HtmlReader(reader);
            //reader2.set_DocType("HTML");
            //reader2.set_InputStream(reader);
            //StringWriter writer = new StringWriter();
            //HtmlWriter writer2 = new HtmlWriter(writer)
            //{
            //    FilterOutput = true
            //};
            //reader2.Read();
            //while (!reader2.EOF)
            //{
            //    writer2.WriteNode((XmlReader)reader2, true);
            //}
            //writer2.Flush();
            //string str2 = Regex.Replace(writer.ToString().Replace("<dd>", "").Replace("</dd>", "").Replace("<>", "").Replace("</>", ""), "<dd.*?>", "").Trim();
            //reader2.Close();
            //reader.Close();
            //reader.Dispose();
            //writer.Close();
            //writer.Dispose();
            //writer2.Close();
            //return str2;
            return "";
        }

        public static string RemoveTag(string input)
        {
            string str = "P|B|I|U|EM|BIG|SMALL|DIV|IMG|SPAN|PRE|BR|HR|UL|OL|LI|DEL|INS|STRONG|A|FONT|DD|DT|TABLE|TD|TR|TBODY";
            str = str + "|" + str.ToLower();
            str = "<[^(" + str + ")].*?>";
            string str2 = input;
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(str2.Replace(@"\n", "").Replace(@"\r", "").Replace("&nbsp;", "").Replace(@"\t", ""), @"<(o|O|w|W|xml|XML)>[\w\W]*?</(o|O|w|W|xml|XML)>", "").Trim(), @"<![\w\W]*?-->", "").Trim(), "<meta.*?>", "").Trim(), @"<(style|STYLE)>[\w\W]*?</(style|STYLE)>", "").Trim();
        }

        public static string RemoveTag(string input, string Exception)
        {
            string str = "P|B|I|U|EM|BIG|SMALL|DIV|IMG|SPAN|PRE|BR|HR|UL|OL|LI|DEL|INS|STRONG|A|FONT|DD|DT|TABLE|TD|TR|TBODY";
            str = str + "|" + str.ToLower();
            str = "<[^(" + str + ")].*?>";
            string str2 = input;
            return Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(Regex.Replace(str2.Replace("\n", "").Replace("\r", "").Replace("&nbsp;", "").Replace("\t", ""), Exception, ""), @"<(o|O|w|W|xml|XML)>[\w\W]*?</(o|O|w|W|xml|XML)>", "").Trim(), @"<![\w\W]*?-->", "").Trim(), "<meta.*?>", "").Trim(), @"<(style|STYLE)>[\w\W]*?</(style|STYLE)>", "").Trim();
        }
    }
}
